package com.prathiba;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Register {

	public static void main(String[] args) throws IOException {

		Order order = new Order();
		// String prod = null;
		BufferedReader b = new BufferedReader(new InputStreamReader(System.in));

		do {
			System.out.println("Enter your option");
			System.out.println("1.Create order");
			System.out.println("2.Print order");
			System.out.println("3.Exit");

			String s = b.readLine();
			if (isNumberValid(s)) {
				int i = Integer.parseInt(s);
				switch (i) {
				case 1:
					while (true) {
						System.out
								.println("Enter the product name[TABLE,CHAIR,PICTURE,LAMP,PLANTS,CLOCK] or Enter QUIT");
						String prod = b.readLine().toUpperCase();
						boolean val = isValueQuit(prod);
						if (val == false) {
							if (Product.exists(prod)) {
								if (order.getOrderLineByProduct(prod) == null) {
									order.addOrderLine(1, Product.valueOf(prod));
								} else {
									order.getOrderLineByProduct(prod)
											.addQuantity(1);
								}
							} else {
								System.out
										.println("Enter a valid product name");
							}
						} else
							break;
					}
					break;
				case 2:
					System.out.println(order);
					break;
				case 3:
					System.exit(1);
					break;
				default:
					System.out.println("Please enter a number from 1-3");
					break;
				}
			}
		} while (true);

	}

	public static boolean isNumberValid(String s) {
		try {
			Integer.valueOf(s);
		} catch (NumberFormatException e) {
			System.out.println("Enter a number");
			return false;
		}
		return true;

	}

	public static boolean isValueQuit(String s) {
		if (s.equalsIgnoreCase("quit"))
			return true;
		else
			return false;

	}
}
